#include "fram_test.h"

/**
 * @brief 运行所有FRAM测试
 */
void FRAM_RunAllTests(void)
{
    printf("\r\n=== 开始FRAM测试套件 ===\r\n");
    
    // 清除所有数据以确保测试环境干净
    if(!FRAM_ClearAllData()) {
        printf("警告：清除FRAM数据失败！\r\n");
    }
    
    printf("\r\n1. 基本功能测试\r\n");
    printf("-----------------\r\n");
    FRAM_TestBasicFunctions();
    HAL_Delay(1000);
    
    printf("\r\n2. 传感器数据测试\r\n");
    printf("-----------------\r\n");
    FRAM_TestSensorData();
    HAL_Delay(1000);
    
    printf("\r\n3. 压力测试\r\n");
    printf("-----------------\r\n");
    FRAM_TestStressTest();
    
    printf("\r\n=== FRAM测试套件完成 ===\r\n");
}

/**
 * @brief 测试FRAM基本读写功能
 */
void FRAM_TestBasicFunctions(void)
{
    uint8_t writeData = 0x55;
    uint8_t readData = 0;
    
    // 1. 测试初始化
    printf("测试初始化... ");
    if(!FRAM_Init()) {
        printf("失败！\r\n");
        return;
    }
    printf("成功\r\n");
    
    // 2. 测试单字节写入和读取
    printf("测试单字节读写... ");
    if(FRAM_WriteByte(0x100, writeData)) {
        if(FRAM_ReadByte(0x100, &readData)) {
            printf("%s\r\n", (writeData == readData) ? "成功" : "失败");
            printf("写入值: 0x%02X, 读取值: 0x%02X\r\n", writeData, readData);
        } else {
            printf("读取失败\r\n");
        }
    } else {
        printf("写入失败\r\n");
    }
    
    // 3. 测试多字节写入和读取
    printf("测试多字节读写... ");
    uint8_t writeBuffer[10] = {1,2,3,4,5,6,7,8,9,10};
    uint8_t readBuffer[10] = {0};
    
    if(FRAM_Write(0x200, writeBuffer, 10)) {
        if(FRAM_Read(0x200, readBuffer, 10)) {
            bool match = true;
            for(int i = 0; i < 10; i++) {
                if(writeBuffer[i] != readBuffer[i]) {
                    match = false;
                    break;
                }
            }
            printf("%s\r\n", match ? "成功" : "失败");
            if(!match) {
                printf("数据不匹配:\r\n");
                printf("写入: ");
                for(int i = 0; i < 10; i++) printf("%02X ", writeBuffer[i]);
                printf("\r\n读取: ");
                for(int i = 0; i < 10; i++) printf("%02X ", readBuffer[i]);
                printf("\r\n");
            }
        } else {
            printf("读取失败\r\n");
        }
    } else {
        printf("写入失败\r\n");
    }
}

/**
 * @brief 测试传感器数据存储功能
 */
void FRAM_TestSensorData(void)
{
    // 创建测试数据
    sensor_data_t testData = {
        .timestamp = HAL_GetTick(),
        .temperature = 25.5f,
        .humidity = 65.0f,
        .co2_level = 400,
        .tvoc_level = 100,
        .ch2o_level = 50,
        .data_valid = 1
    };
    
    // 1. 测试传感器数据保存
    printf("测试传感器数据保存... ");
    if(!FRAM_SaveSensorData(&testData)) {
        printf("失败！\r\n");
        return;
    }
    printf("成功\r\n");
    
    // 2. 测试读取最新数据
    printf("测试读取最新数据... ");
    sensor_data_t readData = {0};
    if(FRAM_GetLatestData(&readData)) {
        printf("成功\r\n");
        printf("读取结果:\r\n");
        printf("- 温度: %.1f°C\r\n", readData.temperature);
        printf("- 湿度: %.1f%%\r\n", readData.humidity);
        printf("- CO2: %dppm\r\n", readData.co2_level);
        printf("- TVOC: %dppb\r\n", readData.tvoc_level);
        printf("- CH2O: %dppb\r\n", readData.ch2o_level);
    } else {
        printf("失败\r\n");
    }
    
    // 3. 测试24小时数据存储
    printf("测试24小时数据存储... ");
    if(FRAM_Save24HourData(&testData)) {
        sensor_data_t hourData[24];
        uint16_t actualCount = 0;
        
        if(FRAM_Get24HourData(hourData, 24, &actualCount)) {
            printf("成功，获取到%d条记录\r\n", actualCount);
        } else {
            printf("读取失败\r\n");
        }
    } else {
        printf("保存失败\r\n");
    }
}

/**
 * @brief 执行FRAM压力测试
 */
void FRAM_TestStressTest(void)
{
    const uint16_t TEST_COUNT = 1000;
    
    // 1. 连续写入测试
    printf("开始连续写入测试 (%d次)...\r\n", TEST_COUNT);
    uint32_t startTime = HAL_GetTick();
    uint16_t successCount = 0;
    
    for(uint16_t i = 0; i < TEST_COUNT; i++) {
        sensor_data_t testData = {
            .timestamp = HAL_GetTick(),
            .temperature = 25.0f + (float)(i % 10),
            .humidity = 60.0f + (float)(i % 20),
            .co2_level = 400 + i,
            .tvoc_level = 100 + i,
            .ch2o_level = 50 + i,
            .data_valid = 1
        };
        
        if(FRAM_SaveSensorData(&testData)) {
            successCount++;
        }
        
        // 每100次操作打印一次进度
        if((i + 1) % 100 == 0) {
            printf("已完成: %d/%d\r\n", i + 1, TEST_COUNT);
        }
    }
    
    uint32_t duration = HAL_GetTick() - startTime;
    float successRate = (float)successCount / TEST_COUNT * 100;
    
    printf("\r\n压力测试结果:\r\n");
    printf("- 总测试次数: %d\r\n", TEST_COUNT);
    printf("- 成功次数: %d\r\n", successCount);
    printf("- 成功率: %.1f%%\r\n", successRate);
    printf("- 总耗时: %ldms\r\n", duration);
    printf("- 平均每次操作耗时: %.2fms\r\n", (float)duration / TEST_COUNT);
    
    // 2. 测试数据完整性
    printf("\r\n数据完整性检查:\r\n");
    uint16_t count = FRAM_GetDataCount();
    printf("- 存储记录总数: %d\r\n", count);
    
    // 读取并验证最后存储的数据
    sensor_data_t lastData;
    if(FRAM_GetLatestData(&lastData)) {
        printf("- 最后一条记录读取成功\r\n");
        printf("  温度: %.1f°C\r\n", lastData.temperature);
        printf("  湿度: %.1f%%\r\n", lastData.humidity);
    } else {
        printf("- 最后一条记录读取失败\r\n");
    }
} 